package com.czh.controller;

import com.czh.dto.UserQueryDTO;
import com.czh.entity.User;
import com.czh.service.UserService;
import com.czh.utils.PageInfo;
import com.czh.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (User) 控制层
 *
 * @author makejava
 * @since 2024-07-24 17:00:54
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询 列表
     *
     * @param user 对象
     * @return
     */
    @PostMapping("/list")
    public List<User> list(@RequestBody User user) {
        List<User> userList = userService.list(user);
        return userList;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/page/list")
    public List<User> pageList(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        List<User> userList = userService.pageList(page, size);
        return userList;
    }


    /**
     * 详情
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/detail")
    public User detail(@RequestParam Long id) {
        return userService.detail(id);
    }

    /**
     * 添加
     *
     * @param user 对象
     * @return
     */
    @PostMapping("/save")
    public User save(@RequestBody User user) {
        userService.save(user);
        return user;
    }

    /**
     * 修改 根据主键更新数据
     *
     * @param user 对象
     * @return
     */
    @PutMapping("/edit")
    public int edit(@RequestBody User user) {
        return userService.edit(user);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("/delete")
    public int delete(@RequestParam Long id) {
        return userService.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids 主键集合
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public int delete(@RequestBody List<Long> ids) {
        int result = 0;
        if (ids != null && ids.size() > 0) {
            result = userService.deleteByIds(ids);
        }
        return result;
    }


    /**
     * 基于注解的分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "page/simple")
    public List<User> pageBySimple(@RequestParam("page") int page, @RequestParam("size") int size) {
        List<User> list = userService.pageBySimple(page, size);
        return list;
    }


    /**
     * 自定义分页
     *
     * @param userQueryDTO
     * @return
     */
    @PostMapping(value = "page/custom")
    public PageUtils pageByCustom(@RequestBody UserQueryDTO userQueryDTO) {
        PageUtils page = userService.pageByCustom(userQueryDTO);
        return page;
    }

    /**
     * 自定义分页
     *
     * @param userQueryDTO
     * @return
     */
    @PostMapping(value = "page/custom2")
    public PageInfo pageByCustom2(@RequestBody UserQueryDTO userQueryDTO) {
        PageInfo page = userService.pageByCustom2(userQueryDTO);
        return page;
    }
}

